package main

import "fmt"

//#link: https://leetcode-cn.com/problems/count-triplets-that-can-form-two-arrays-of-equal-xor/
func main() {
	arr := []int{2, 3, 1, 6, 7}
	fmt.Println(countTriplets(arr))
}

//找规律 利用前缀和
//参考题解: https://leetcode-cn.com/problems/count-triplets-that-can-form-two-arrays-of-equal-xor/solution/xing-cheng-liang-ge-yi-huo-xiang-deng-sh-jud0/
func countTriplets(arr []int) int {
	result := 0
	arrXor := make([]int, len(arr)+1)
	for i := 0; i < len(arr); i++ {
		arrXor[i+1] = arrXor[i] ^ arr[i]
	}

	for i := 0; i < len(arr); i++ {
		for k := i + 1; k < len(arr); k++ {
			//当相等时，则k-i范围里的所有值都可代表j
			if arrXor[i] == arrXor[k+1] {
				result += k - i
			}
		}
	}
	return result
}
